package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) { //dishId   setmealId   dishFlavor
        //1. 判断用户的购物车中是否包含商品
        // select * from shopping_cart where user_id=? and dish_id=? and setmeal_id=? and dish_flavor=?
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .dishId(shoppingCartDTO.getDishId())
                .setmealId(shoppingCartDTO.getSetmealId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {
            //2. 如果包含，更新操作，数量+1
            ShoppingCart dbCart = list.get(0); //dbCart表示数据库中的一条数据
            dbCart.setNumber(dbCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(dbCart);
        } else {
            //3. 如果不包含，添加操作，数量默认1
            //补全信息
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1); //默认数量1
            if (shoppingCart.getDishId() != null) {
                //根据菜品id查询菜品信息
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //根据套餐id查询套餐信息
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //添加购物项
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看用户购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return  shoppingCartMapper.list(
                ShoppingCart.builder()
                        .userId(BaseContext.getCurrentId())
                        .build()
        );
    }

    /**
     * 清空用户购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}